package com.softeng.network.chess;


import javax.swing.JFrame;


public class King extends ChessPiece{

	public boolean isChecked;

	public boolean firstMove; 

	//-----------------------------------------------------------------------------------------------

	King(String color){

		idString = "king";
		this.color = color;
		this.isChecked = false;
		this.firstMove = true;

		if (color == "white"){
			this.pieceString = "resources/assets/king_white.gif";
		}
		else{
			this.pieceString = "resources/assets/king_black.gif";
		}

	}

	//-----------------------------------------------------------------------------------------------

	King(String color, int position, JFrame window){

		this(color);

		addPieceImage(position, window, this.pieceString);

	}

	//-----------------------------------------------------------------------------------------------

	// Sees if a particular move is a move that a King is allowed to make
	public boolean validMove(int currentPosition, int position, ChessPiece[] boardArray){

		boolean validMove = false;
		int currentRow = getRowFromPos(currentPosition);
		int currentCol = getColFromPos(currentPosition);
		int nextRow = getRowFromPos(position);
		int nextCol = getColFromPos(position); 

		//moving 1 space forward
		if(nextRow == (currentRow + 1) && nextCol == currentCol && (boardArray[position] == null || !boardArray[currentPosition].color.equals(boardArray[position].color))){
			validMove = true; 
			System.out.println("King moves, valid move = true (1 up)");
			firstMove = false;
		}
		//moving 1 space back
		else if(nextRow == (currentRow - 1) && nextCol == currentCol && (boardArray[position] == null || !boardArray[currentPosition].color.equals(boardArray[position].color))){
			validMove = true; 
			System.out.println("King moves, valid move = true (1 down)");
			firstMove = false;
		}
		//moving 1 space left
		else if(nextRow == currentRow && (nextCol == currentCol - 1) && (boardArray[position] == null || !boardArray[currentPosition].color.equals(boardArray[position].color))){
			validMove = true; 
			System.out.println("King moves, valid move = true (1 left)");
			firstMove = false;
		}
		//moving 1 space right
		else if(nextRow == currentRow && (nextCol == currentCol + 1) && (boardArray[position] == null || !boardArray[currentPosition].color.equals(boardArray[position].color))){
			validMove = true; 
			System.out.println("King moves, valid move = true (1 right)");
			firstMove = false;
		}

		//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

		//moving 1 space up and left
		else if((nextRow == currentRow + 1) && (nextCol == currentCol - 1) && (boardArray[position] == null || !boardArray[currentPosition].color.equals(boardArray[position].color))){
			validMove = true; 
			System.out.println("King moves, valid move = true (1 up and left)");
			firstMove = false;
		}
		//moving 1 space up and right
		else if((nextRow == currentRow + 1) && (nextCol == currentCol + 1) && (boardArray[position] == null || !boardArray[currentPosition].color.equals(boardArray[position].color))){
			validMove = true; 
			System.out.println("King moves, valid move = true (1 up and right)");
			firstMove = false;
		}
		//moving 1 space down and left
		else if((nextRow == currentRow - 1) && (nextCol == currentCol - 1) && (boardArray[position] == null || !boardArray[currentPosition].color.equals(boardArray[position].color))){
			validMove = true; 
			System.out.println("King moves, valid move = true (1 down and left)");
			firstMove = false;
		}
		//moving 1 space down and right
		else if((nextRow == currentRow - 1) && (nextCol == currentCol + 1) && (boardArray[position] == null || !boardArray[currentPosition].color.equals(boardArray[position].color))){
			validMove = true; 
			System.out.println("King moves, valid move = true (1 down and right)");
			firstMove = false;
		}
		//Castling valid moves
		else if(Chess.getInstance(false).myColor.equals("white") && firstMove == true && nextRow == currentRow && nextCol == currentCol - 2 && boardArray[currentPosition-4].idString.equals("rook") && ((Rook)boardArray[currentPosition-4]).firstMove == true && !piecesBetweenKingRook(currentPosition,currentPosition-4,boardArray)){
			validMove = true;
			System.out.println(boardArray[currentPosition].color + " King moves, castling to the left.");
		}
		else if(Chess.getInstance(false).myColor.equals("white") && firstMove == true && nextRow == currentRow && nextCol == currentCol + 2 && boardArray[currentPosition+3].idString.equals("rook") && ((Rook)boardArray[currentPosition+3]).firstMove == true && !piecesBetweenKingRook(currentPosition,currentPosition+3,boardArray)){
			validMove = true;
			System.out.println(boardArray[currentPosition].color + " King moves, castling to the right.");
		}
		else if(Chess.getInstance(false).myColor.equals("black") && firstMove == true && nextRow == currentRow && nextCol == currentCol - 2 && boardArray[currentPosition-3].idString.equals("rook") && ((Rook)boardArray[currentPosition-3]).firstMove == true && !piecesBetweenKingRook(currentPosition,currentPosition-3,boardArray)){
			validMove = true;
			System.out.println(boardArray[currentPosition].color + " King moves, castling to the left.");
		}
		else if(Chess.getInstance(false).myColor.equals("black") && firstMove == true && nextRow == currentRow && nextCol == currentCol + 2 && boardArray[currentPosition+4].idString.equals("rook") && ((Rook)boardArray[currentPosition+4]).firstMove == true && !piecesBetweenKingRook(currentPosition,currentPosition+4,boardArray)){
			validMove = true;
			System.out.println(boardArray[currentPosition].color + " King moves, castling to the right.");
		}
		else{
			
			validMove = false; 
		
		}
		
		return validMove; 

	}
	
	public boolean piecesBetweenKingRook(int currentPosition, int index, ChessPiece[] boardArray) {
		
		boolean piecesBetween = false;
		
		if (currentPosition > index){
			for (int i = currentPosition-1; i > index; i--){
				if (boardArray[i] != null){
					piecesBetween = true;
				}
			}
		}
		if (currentPosition < index){
			for (int i = currentPosition+1; i < index; i++){
				if (boardArray[i] != null){
					piecesBetween = true;
				}
			}
		}
		
		return piecesBetween;
	}

	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// tells if the king is currently in "check"
	public boolean isCurrentlyChecked(){

		return isChecked;

	}

}

